package com.jadcon.bedbreakfast.web;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.bedbreakfast.domain.Reservation;
import com.jadcon.bedbreakfast.domain.ReservationService;
import com.jadcon.bedbreakfast.domain.ReservationValidator;

@Controller
@SessionAttributes("newReservation")
public class ReservationController {

	@Autowired
	private ReservationService reservationService;


	@RequestMapping("/reservationDetail.html")
	public ModelAndView reservationDetail() throws SQLException, ParseException {
		System.out
				.println("ReservationController.reservationDetail() begin...");
		// get data from model
		List<Reservation> reservationList = reservationService
				.getReservationList();

		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationDetail");
		mav.addObject("reservationList", reservationList);
		System.out.println("added object to reservation list...");
		return mav;
		// map ("key", value)

	}

	@RequestMapping("/reservationsearch.html")
	public ModelAndView reservationSearch(@RequestParam int reservationNumber) throws SQLException, ParseException {
		System.out
				.println("ReservationController.reservationSearch() begin...");
		
		// get data from model
		Reservation reservation = reservationService
				.findByNumber(reservationNumber);

		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationDetail2"); // "reservationDetail2"
		// is the
		// view name
		mav.addObject("reservation", reservation);
		return mav;
	}

	// map ("key", value)

	@RequestMapping("/findReservationByRoom.html")
	public ModelAndView findReservationByRoom(@RequestParam int roomNumber) throws SQLException, ParseException {
		System.out
				.println("ReservationController.reservationRoomSearch() begin...");

		// get data from model
		List<Reservation> reservationList = reservationService
				.findReservationByRoom(roomNumber);

		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("reservationDetail3"); // "reservationDetail2"
		// is the
		// view name
		mav.addObject("reservationList", reservationList);
		return mav;
	}

	

	@RequestMapping("/thanks.html")
	public ModelAndView thanks() {
		System.out.println("ReservationController.thanks() begin...");
		return new ModelAndView("thanks");
	}

	@RequestMapping("/findReservationByStartDate.html")
	public ModelAndView findReservationByStartDate(@RequestParam String checkIn)
			throws ParseException, InterruptedException, SQLException {
		System.out
				.println("ReservationController.findReservationByStartDate() begin...");

		// get data from model
		List<Reservation> reservationList = reservationService
				.findReservationByStartDate(checkIn);

		// add data to ModelAndView
		ModelAndView mav = new ModelAndView("findReservationByStartDate");
		mav.addObject("reservationList", reservationList);
		return mav;

	}

	@RequestMapping("/reservation.html")
	public ModelAndView addReservation() {

		Reservation newReservation = new Reservation();
		ModelAndView mav = new ModelAndView("addReservation");
		mav.addObject("newReservation", newReservation);
		return mav;

	}
	

	@RequestMapping("/reservation2.html")
	public ModelAndView confirmReservation(@RequestParam String submit,
			@ModelAttribute("newReservation") Reservation newReservation, BindingResult result) throws SQLException {
		
		System.out
				.println("ReservationController.confirmReservation() begin...");

		ReservationValidator reservationValidator = new ReservationValidator();
		reservationValidator.validate(newReservation, result);
		if (result.hasErrors()){
			return new ModelAndView("addReservation"); 
		}

		ModelAndView mav = new ModelAndView("reservation2"); 
		mav.addObject("newReservation", newReservation); 
		return mav;
	}

	// method that handles the form submission
	@RequestMapping("/saveReservation.html")
	public ModelAndView saveNewReservation(
			@ModelAttribute("newReservation") Reservation newReservation) throws SQLException {

		System.out.println("ReservationController.saveReservation() begin: "
				+ newReservation);
		reservationService.save(newReservation);
		ModelAndView mav = new ModelAndView("thanks"); // points to "tiles" def
		// thanks
		mav.addObject(newReservation);
		return mav;
	}
	
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }


}
